package org.jeecg.basic.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.basic.dto.MesLineOpStnDTO;
import org.jeecg.basic.entity.MesLineOpCfg;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.basic.entity.MesLineStnCfg;
import org.jeecg.basic.service.IMesLineStnCfgService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 工位配置
 * @Author: LiCY
 * @Date: 2023-06-17
 * @Version: V1.0
 */
@Api(tags = "工位配置")
@RestController
@RequestMapping("/basic/mesLineStnCfg")
@Slf4j
public class MesLineStnCfgController extends JeecgController<MesLineStnCfg, IMesLineStnCfgService> {
    @Autowired
    private IMesLineStnCfgService mesLineStnCfgService;

    /**
     * 分页列表查询666
     *
     * @param mesLineStnCfg
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "工位配置-分页列表查询")
    @ApiOperation(value = "工位配置-分页列表查询", notes = "工位配置-分页列表查询")
    @GetMapping(value = "/list")
    @DS("#header.his")
    public Result<IPage<MesLineStnCfg>> queryPageList(MesLineStnCfg mesLineStnCfg,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<MesLineStnCfg> queryWrapper = QueryGenerator.initQueryWrapper(mesLineStnCfg, req.getParameterMap());
        Page<MesLineStnCfg> page = new Page<MesLineStnCfg>(pageNo, pageSize);
        queryWrapper.orderByAsc("sequence");
        IPage<MesLineStnCfg> pageList = mesLineStnCfgService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 手工线工站列表查询
     *
     * @return
     */
    //@AutoLog(value = "mes_line_op_cfg-分页列表查询")
    @ApiOperation(value = "手工线工站列表查询", notes = "手工线工站列表查询")
    @GetMapping(value = "/queryManualList")
    @DS("#header.his")
    public Result<List<MesLineOpStnDTO>> queryManualList() {
        List<MesLineOpStnDTO> pageList = mesLineStnCfgService.queryManualList();
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param mesLineStnCfg
     * @return
     */
    @AutoLog(value = "工位配置-添加")
    @ApiOperation(value = "工位配置-添加", notes = "工位配置-添加")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_line_stn_cfg:add")
    @PostMapping(value = "/add")
    @DS("#header.line")
    public Result<String> add(@RequestBody MesLineStnCfg mesLineStnCfg) {
        mesLineStnCfgService.save(mesLineStnCfg);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mesLineStnCfg
     * @return
     */
    @AutoLog(value = "工位配置-编辑")
    @ApiOperation(value = "工位配置-编辑", notes = "工位配置-编辑")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_line_stn_cfg:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("#header.line")
    public Result<String> edit(@RequestBody MesLineStnCfg mesLineStnCfg) {
        mesLineStnCfgService.updateByMultiId(mesLineStnCfg);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过选中对象删除
     *
     * @param mesLineStnCfg
     * @return
     */
    @AutoLog(value = "工位配置-通过id删除")
    @ApiOperation(value = "工位配置-通过id删除", notes = "工位配置-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_line_stn_cfg:delete")
    @RequestMapping(value = "/delete")
    public Result<String> delete(@RequestBody MesLineStnCfg mesLineStnCfg) {
        mesLineStnCfgService.deleteByMultiId(mesLineStnCfg);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @AutoLog(value = "工位配置-批量删除")
    @ApiOperation(value = "工位配置-批量删除", notes = "工位配置-批量删除")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_line_stn_cfg:deleteBatch")
    @RequestMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestBody List<MesLineStnCfg> list) {
        list.forEach(item -> {
            mesLineStnCfgService.deleteByMultiId(item);
        });
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "工位配置-通过id查询")
    @ApiOperation(value = "工位配置-通过id查询", notes = "工位配置-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("#header.his")
    public Result<MesLineStnCfg> queryById(@RequestParam(name = "id", required = true) String id) {
        MesLineStnCfg mesLineStnCfg = mesLineStnCfgService.getById(id);
        if (mesLineStnCfg == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(mesLineStnCfg);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mesLineStnCfg
     */
    //@RequiresPermissions("org.jeecg.modules.demo:mes_line_stn_cfg:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, MesLineStnCfg mesLineStnCfg) {
        return super.exportXls(request, mesLineStnCfg, MesLineStnCfg.class, "工位配置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("mes_line_stn_cfg:importExcel")
    @DS("#header.line")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MesLineStnCfg.class);
    }

    /**
     * 通过op查询
     *
     * @param op
     * @return
     */
    //@AutoLog(value = "工位配置-通过op查询")
    @ApiOperation(value = "工位配置-通过op查询", notes = "工位配置-通过op查询")
    @GetMapping(value = "/stnSelect")
    @DS("#header.his")
    public Result<List<MesLineStnCfg>> stnSelect(
            @RequestParam(name = "line", required = true) String line,
            @RequestParam(name = "op", required = true) String op) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("line", line);
        map.put("op", op);
        List<MesLineStnCfg> stnSelects = mesLineStnCfgService.listByMap(map);
        if (stnSelects.isEmpty()) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(stnSelects);
    }

}
